﻿using System.Windows.Controls;
using System.Windows.Media;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms.Design;

namespace UIPrototyper
{
    class ImageProperties : Property, IChanged
    {
        private CustomImage image;
        private string sourceURI;

        public ImageProperties(CustomImage pImage)
        {
            this.image = pImage;
        }

        [DescriptionAttribute("O nome da imagem"),
        CategoryAttribute("Image")]
        public override string Nome
        {
            get { return this.image.Name; }
            set { this.image.Name = value; LaunchChangedEvent(this.image); }
        }

        [DescriptionAttribute("Como a imagem irá se comportar ao ser esticada."),
        CategoryAttribute("Image")]
        public Stretch Stretch
        {
            get { return this.image.Stretch; }
            set { this.image.Stretch = value; LaunchChangedEvent(this.image); }
        }

        [Editor(typeof(FileNameEditor), typeof(UITypeEditor)),
        DescriptionAttribute("A Imagem que será apresentada."),
        CategoryAttribute("Image")]
        public string Source
        {
            get { return this.sourceURI; }
            set
            {
                sourceURI = value;

                if (!string.IsNullOrEmpty(sourceURI))
                {
                    try
                    {
                        this.image.Source = (ImageSource)(new ImageSourceConverter()).ConvertFrom(sourceURI);
                    }
                    catch (System.Exception)
                    {
                        this.image.Source = null;
                        System.Windows.Forms.MessageBox.Show("Imagem não encontrada!");
                    }
                }
                else
                {
                    this.image.Source = null;
                }

                LaunchChangedEvent(this.image);
            }
        }

        #region IChanged Members

        public event ChangedHandler iChanged;

        public void LaunchChangedEvent(IChanged pChangedObject)
        {
            if (iChanged != null)
            {
                iChanged(pChangedObject);
            }
        }

        #endregion
    }
}
